Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document type helpers for oneOfs #1023

Merged
merged 1 commit into from
Mar 29, 2024
Merged

Document type helpers for oneOfs #1023

merged 1 commit into from
Mar 29, 2024

Conversation

bhollis
Copy link
Contributor

@bhollis bhollis commented Mar 29, 2024

Add a section to the documentation that shows some TypeScript types that could be useful in working with union-style oneOfs. I've written these a few times in different projects so I could properly type helper functions that operate on generated oneOfs, and I figured it might be useful to other users of ts-proto.

As an example, given the following:

interface MusicPlayerRequest {
  command?:
    | { $case: "start"; start: StartCommand }
    | { $case: "stop"; stop: StopCommand }
    | { $case: "next"; list: NextCommand }
    | undefined; 
}

Then the helpers can be used like this:

type MusicPlayerCommandNames = OneOfCases<MusicPlayerRequest['command']>; // = "start" | "stop" | "next"

type MusicPlayerCommands = OneOfValues<MusicPlayerRequest['command']>; // = StartCommand | StopCommand | NextCommand

type NextCommandByName = OneOfCase<MusicPlayerRequest['command'], 'next'> // = NextCommand

function sendCommand<C extends MusicPlayerCommandNames>(
  commandName: MusicPlayerCommandNames, 
  command: OneOfCase<MusicPlayerRequest['command'], C>
) {
  ...
}

// The `command` argument is automatically of type NextCommand because the `commandName` argument is "next"
sendCommand("next", { skip: 1 }) 

Add a section to the documentation that shows some TypeScript types that could be useful in working with union-style oneOfs.
@stephenh
Copy link
Owner

Looks great, thanks @bhollis !

@stephenh stephenh merged commit 9727bba into stephenh:main Mar 29, 2024
1 check failed
@stephenh
Copy link
Owner

🎉 This PR is included in version 1.171.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

stephenh pushed a commit that referenced this pull request Apr 19, 2024
ref: #1023

In previous helper, it is not possible to construct a message with the
oneof field passed as a function argument.

This helper allows for strict type checking and construct message using
oneof fields as an argument.

Example

```proto
syntax = "proto3";

package logs;

message SignUp {
    string name = 1;
}

message Login {
    string account_id = 1;
}

message Logout {
    string account_id = 1;
    string reason = 2;
}

message LogPayload {
  string created_at = 1;
  oneof payload {
    SignUp signup = 2;
    Login login = 3;
    Logout lougout = 4;
  }
}
```

```typesecript
// pass by argument
foo<P extends PaloadNames>(
  payload: OneOfValue<LogPayload['payload'], P>
) {
  const req: LogPayload = {
    createdAt: new Date().toUTCString(),
    payload,
  };

  send(req);
}
```

```typescript
// Usage

// Good
this.foo( {
  $case: 'login',
  login: {
    accountId: 'accountId',
  },
});

// Build Fail because case and field is not matched
// Argument of type '{ $case: string; login: { accountId: string; }; }' is not assignable to parameter of type 'never'.
this.foo( {
  $case: 'logout',
  login: {
    accountId: 'accountId',
  },
});
```

Signed-off-by: clucle <wjdenwls123@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants